//地址编辑页
const app = getApp();
var server = require('../../utils/server');
var common = require('../../utils/common');
var hideTimer = null;
Page({
  data: {
    userDefinedLable: "",
    laSeleted: "",
    showLabelPop: false,
    showNearBy: false,
    newLabel: "",
    addressInfo: {},
    mobile: "",
    cityid: "",
    curCity: '',
    addrId: null,
    nearbyBuildings: [],
    showAddrStatus: false,
    address: "",
    resultBuildings: [],
    addrFromType: "",
    showCity: false,
    animationData: [],
    cityList: [],
    curstoreid: '',
    pname: "",
    cityname: "",
    adname: "",
    isSaving: false
  },
  onLoad: function(options = {}) {
    this.setData({
      addrId: options.addrId,
      addrFromType: options.addrType
    })

  },
  onShow() {
    if (this.data.addrId != "undefined" && this.data.addrId != "") {
      wx.setNavigationBarTitle({
        title: '编辑地址'
      })
      wx.getStorage({
        key: 'curAddrInfo',
        success: res => {
          var data = res.data;
          this.data.cityid = data.city_id;
          this.data.userlatitude = data.latitude;
          this.data.userlongitude = data.longitude;
          this.data.pname = data.pname == undefined ? '' : data.pname;
          this.data.cityname = data.cityname == undefined ? '' : data.cityname;
          this.data.adname = data.adname == undefined ? '' : data.adname;
          this.setData({
            addressInfo: data,
            mobile: data.mobile,
            address: data.address,
            house_number: data.house_number,
          })
          if (app.globalData.editAddrLatitude != "" && app.globalData.editAddrLongitude != "" && app.globalData.editAddrLocation != "") {
            this.setData({
              address: app.globalData.editAddrLocation,
              curCity: app.globalData.editAddrCity == "" ? data.city : app.globalData.editAddrCity
            })
            this.data.userlatitude = app.globalData.editAddrLatitude;
            this.data.userlatitude = app.globalData.editAddrLatitude;
            this.data.userlongitude = app.globalData.editAddrLongitude;
            this.data.pname = app.globalData.editAddrPname == "" ? this.data.pname : app.globalData.editAddrPname;
            this.data.cityname = app.globalData.editAddrCityname == "" ? this.data.cityname : app.globalData.editAddrCityname;
            this.data.adname = app.globalData.editAddrAdname == "" ? this.data.adname : app.globalData.editAddrAdname;
            this.data.cityid = app.globalData.editAddrCityId == "" ? this.data.cityid : app.globalData.editAddrCityId;
          } else {
            this.setData({
              curCity: data.city
            })
          }
          if (data.tag == "家") {
            this.setData({
              laSeleted: "0"
            })
          } else if (data.tag == "公司") {
            this.setData({
              laSeleted: "1"
            })
          } else {
            if (data.tag != "") {
              this.setData({
                laSeleted: "2",
                userDefinedLable: data.tag
              })
            }
          }
        }
      })
    } else {
      this.setData({
        address: app.globalData.editAddrLocation,
        curCity: app.globalData.editAddrCity == "" ? app.globalData.locateCity : app.globalData.editAddrCity
      })
      this.data.userlatitude = app.globalData.editAddrLatitude;
      this.data.userlatitude = app.globalData.editAddrLatitude;
      this.data.userlongitude = app.globalData.editAddrLongitude;
      this.data.pname = app.globalData.editAddrPname;
      this.data.cityname = app.globalData.editAddrCityname;
      this.data.adname = app.globalData.editAddrAdname;
      if (app.globalData.editAddrCityId == "") {
        server.postJSON(common.storePath + 'address/getcityid/', {
          "city": app.globalData.locateCity
        }, res => {
          if (res.data.errno == "0") {
            this.data.cityid = res.data.data;
          } else {
            common.showErrorToast(res.data.errmsg);
          }
        })
      } else {
        this.data.cityid = app.globalData.editAddrCityId;
      }
      wx.getStorage({
        key: 'userInfo',
        success: res => {
          this.setData({
            mobile: this.data.mobile == "" ? res.data.member.cellphone : this.data.mobile,
          })
        },
        fail: res => {
        }
      })
      wx.setNavigationBarTitle({
        title: '新建地址'
      })
    }
  },
  onUnload() {
    app.globalData.editAddrLatitude = "";
    app.globalData.editAddrLongitude = "";
    app.globalData.editAddrLocation = "";
    app.globalData.editAddrCity = "";
    clearTimeout(hideTimer);
    wx.removeStorage({
      key: 'curAddrInfo',
      success: function(res) {
      },
    })
  },
  onHide() {
    clearTimeout(hideTimer);
    app.globalData.editAddrLatitude = "";
    app.globalData.editAddrLongitude = "";
    app.globalData.editAddrLocation = "";
  },
  formSubmit: function(e) {
    if (e.detail.value.mobile == "") {
      common.showToast("请输入手机号")
    } else if (e.detail.value.consignee == "" || e.detail.value.consignee.length < 2) {
      common.showToast("请输入收货人姓名")

    } else if (this.data.address == "") {
      common.showToast("请输入详细地址")
    } else if (e.detail.value.house_number == "") {
      common.showToast("请输入门牌号")
    } else {
      var tag = "";
      if (this.data.laSeleted == "0") {
        tag = "家";
      } else if (this.data.laSeleted == "1") {
        tag = "公司";
      } else if (this.data.laSeleted == "2") {
        tag = this.data.userDefinedLable
      }
      var editParam = {
        "mobile": e.detail.value.mobile,
        "consignee": e.detail.value.consignee,
        "address": this.data.address,
        "house_number": e.detail.value.house_number,
        "address_id": this.data.addrId,
        "tag": tag,
        "city_id": this.data.cityid,
        "latitude": this.data.userlatitude,
        "longitude": this.data.userlongitude,
        "pname": this.data.pname,
        "cityname": this.data.cityname,
        "adname": this.data.adname
      }
      if (this.data.addrId != "undefined" && this.data.addrId != "") {
        this.setData({
          isSaving: true
        })
        server.postJSON(common.storePath + 'address/update/', editParam, res => {
          this.setData({
            isSaving: false
          })
          if (res.data.errno == "0") {
            wx.showLoading({
              title: '',
              icon: 'none'
            })
            if (this.data.addrFromType == '1') { //从到家业务结算页进入
              app.globalData.addrId = this.data.addrId;
              app.globalData.skipType = 1;
              app.globalData.longitude = this.data.userlongitude;
              app.globalData.latitude = this.data.userlatitude;
              app.globalData.city = this.data.curCity;
              app.globalData.location = e.detail.value.address;
              wx.getStorage({
                key: 'orderInfo',
                success: res => {
                  res.data.goods.score_delivery_checked = 0;
                  wx.setStorage({
                    key: 'orderInfo',
                    data: res.data,
                    success: resp => {
                      wx.navigateBack();
                    }
                  })
                },
              })
            } else if (this.data.addrFromType == '3') { //从购物车进
              app.globalData.addrId = this.data.addrId;
              app.globalData.skipType = 1;
              app.globalData.longitude = this.data.userlongitude;
              app.globalData.latitude = this.data.userlatitude;
              app.globalData.city = this.data.curCity;
              app.globalData.location = e.detail.value.address;
              app.globalData.isGetCartByAdd = true;
              if (this.data.curstoreid != '') {
                wx.setStorage({
                  key: 'storeId',
                  data: this.data.curstoreid,
                  success: res => {
                    wx.navigateBack();
                  },
                  fail: res => {
                    wx.navigateBack();
                  }
                })
              } else {
                wx.navigateBack();
              }

            } else if (this.data.addrFromType == '4') { //修改地址返回结算页
              app.globalData.isEditToAccount = true;
              wx.navigateBack();
            } else {
              wx.navigateBack();
            }
          } else {
            common.showErrorToast(res.data.errmsg);
          }
        })
      } else {
        this.setData({
          isSaving: true
        })
        server.postJSON(common.storePath + 'address/add/', editParam, res => {
          this.setData({
            isSaving: false
          })
          if (res.data.errno == "0") {
            wx.showLoading({
              title: '',
              icon: 'none'
            })
            if (this.data.addrFromType == '1') {
              app.globalData.addrId = res.data.data;
              app.globalData.longitude = this.data.userlongitude;
              app.globalData.latitude = this.data.userlatitude;
              app.globalData.city = this.data.curCity;
              app.globalData.location = e.detail.value.address;
              app.globalData.skipType = 1;
              wx.getStorage({
                key: 'orderInfo',
                success: res => {
                  wx.setStorage({
                    key: 'orderInfo',
                    data: res.data,
                    success: resp => {
                      wx.navigateBack();
                    }
                  })
                },
                fail: res => {
                  wx.navigateBack();
                }
              })
            } else if (this.data.addrFromType == '3') { //从购物车进
              app.globalData.addrId = res.data.data;
              app.globalData.skipType = 1;
              app.globalData.longitude = this.data.userlongitude;
              app.globalData.latitude = this.data.userlatitude;
              app.globalData.city = this.data.curCity;
              app.globalData.location = e.detail.value.address;
              app.globalData.isGetCartByAdd = true;
              wx.setStorage({
                key: 'storeId',
                data: this.data.curstoreid,
                success: res => {
                  wx.navigateBack();
                },
                fail: res => {
                  wx.navigateBack();
                }
              })
            } else if (this.data.addrFromType == '2') { //修改地址返回生鲜到家页
              app.globalData.addrId = res.data.data;
              app.globalData.longitude = this.data.userlongitude;
              app.globalData.latitude = this.data.userlatitude;
              app.globalData.location = e.detail.value.address;
              app.globalData.city = this.data.curCity;
              app.globalData.skipType = 1;
              wx.navigateBack();
            } else if (this.data.addrFromType == '4') { //修改地址返回结算页
              app.globalData.isEditToAccount = true;
              wx.navigateBack();
            } else {
              wx.navigateBack();
            }
          } else {
            common.showErrorToast(res.data.errmsg);
          }
        })
      }
    }
  },
  getNewTel(e) {
    if (e.detail.value != this.data.mobile) {
      this.data.mobile = e.detail.value;
      wx.getStorage({
        key: 'curAddrInfo',
        success: function(res) {
          res.data.mobile = e.detail.value;
          wx.setStorage({
            key: 'curAddrInfo',
            data: res.data
          })
        },
      })
    }
  },
  // 显示遮罩层
  getNewAddr: function() {
    wx.navigateTo({
      url: 'changeLocation',
    })
  },
  //选择地址标签
  chooseAddrLa(e) {
    this.setData({
      laSeleted: e.currentTarget.dataset.id
    })
  },
  userDefinedLa() {
    this.setData({
      showLabelPop: !this.data.showLabelPop
    })
  },
  inputNewLabel(e) {
    var reg = /[^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n]/g;
    var val = e.detail.value;
    if (val.match(reg)) {
      val = val.replace(reg, '');
    }
    this.setData({
      newLabel: val
    })
  },
  addExtraLabel() {
    if (this.data.newLabel != "") {
      this.setData({
        laSeleted: "2",
        userDefinedLable: this.data.newLabel
      })
    }
    this.setData({
      showLabelPop: !this.data.showLabelPop,
    })
  },
  hideLabelPop() {
    this.setData({
      showLabelPop: !this.data.showLabelPop,
    })
  },
  hideToast() {
    hideTimer = setTimeout(function() {
      wx.hideToast()
    }, 3500)
  },
  showCityList() {
    wx.navigateTo({
      url: '../../pages/common/cityList?fromtype=editaddr',
    })
  },
  //删除地址
  deleteAddress: function() {
    this.setData({
      showDelPop: true
    })
  },
  confirmDel() {
    if (this.data.addrId == app.globalData.addrId) {
      common.showToast("无法删除当前选中的收货地址，请切换后再删除该地址");
      this.hideToast();
    } else {
      server.postJSON(common.storePath + 'address/delete/', {
        "address_id": this.data.addrId
      }, res => {
        if (res.data.errno == "0") {
          this.setData({
            showDelPop: false
          })
          wx.navigateBack();
        } else {
          common.showErrorToast(res.data.errmsg);
        }
      })
    }
  },
  cancelDel() {
    this.setData({
      showDelPop: false
    })
  }
})